package com.com.knowledgepaymentmanagementsystem.controller.goods;

import com.com.knowledgepaymentmanagementsystem.pojo.goods.Goods;
import com.com.knowledgepaymentmanagementsystem.service.goods.GoodsService;
import com.com.knowledgepaymentmanagementsystem.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author DELL
 * @version 1.0
 * @description: TODO
 * @date 2025/9/22 15:27
 */
@RestController
@CrossOrigin
@Api(tags = "商品管理")
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;  // 修正Service名称和类型

    @ApiOperation(value = "查询商品",httpMethod = "POST",notes = "查询商品")
    @PostMapping("/find")
    public ResultUtil<List<Goods>> getGoods(@RequestBody Goods goods){
        List<Goods> goodsList = goodsService.getGoods(goods);
        try {
            return new  ResultUtil<>(200,"查询成功",goodsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new  ResultUtil<>(200,"查询失败");

        }
    }

    @ApiOperation(value = "添加商品",httpMethod = "POST",notes = "添加商品")
    @PostMapping("/insert")
    public ResultUtil<Object> insertGoods(@RequestBody Goods goods){
        try {
            return new ResultUtil<>(200,"新增成功",goodsService.insertGoods(goods));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500,"新增失败");
        }
    }


    @ApiOperation(value = "更新商品",httpMethod = "PUT",notes = "修改图文课程")
    @PutMapping("/update")
    public ResultUtil updateGoods(@RequestBody Goods goods){

        goodsService.updateGood(goods);
        try {
            return new ResultUtil(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500,"修改失败");

        }
    }



    @ApiOperation(value = "删除商品",httpMethod = "DELETE",notes = "删除商品")
    @DeleteMapping("/delete/{id}")
    public ResultUtil deleteGoods(@PathVariable Integer id){
        try {
            goodsService.deleteGoods(id);
            return new ResultUtil(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(500,"删除失败");
        }
    }

}